package hadoop_example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;

public class FileSystemAPIDemo {
    public static void main(String[] args) {
        Configuration configuration=new Configuration();
        configuration.set("fs.defaultFS","hdfs://wangxin:8020");
        try {
            FileSystem fileSystem =FileSystem.get(configuration);
            Path rootpath=new Path("hdfs://wangxin:8020/");
            FileStatus [] children =fileSystem.listStatus(rootpath);
            for(FileStatus fs:children){
                System.out.println(fs.getPath().getName());
            }
            Path filepath=new Path("hdfs://wangxin:8020/A/test7.txt");
            if (fileSystem.exists(filepath)){
                fileSystem.delete(filepath,false);
            }
            FSDataOutputStream fso=fileSystem.create(filepath);
            fso.writeUTF("the");
            fso.close();

            FSDataInputStream fsi=fileSystem.open(filepath);
            String hello=fsi.readUTF();
            System.out.println(hello);
            fsi.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}